/*
	This file is part of AVRCppLib.

    AVRCppLib is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AVRCppLib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AVRCppLib.  If not, see <http://www.gnu.org/licenses/>.

    Copyright (C) Maxime Lemonnier 2007-2011
 */

#ifndef SensorSerializer_H_
#define SensorSerializer_H_

#include "ADC/ADCHardware.h"
#include "DataStructures/Dictionary.h"
#include "Coms/USART/ActiveBuffered/ActiveBufferedUSART.h"
#include "DataStructures/ActiveBuffers/ActiveTransmitBuffers/TransmitActiveStringBuffer.h"
#include "DataStructures/ActiveBuffers/ActiveReceiveBuffers/ReceiveEOLActiveStringBuffer.h"
#include "../../RegularExpressions/RegularExpression.h"
#include "Timers/Timer16bits3OCR.h"
#include "../Patterns/SamplingPattern.h"
#include "../../Sensors/ADCSensor.h"
#include "SerializableSample.h"

using ADCs::ADCHardware;
using ADCs::ChannelMode;
using Coms::USART::ActiveBufferedUSART;
using Timers::Timer16bits3OCR;
using RegularExpressions::RegularExpression;

#define BUFFERS_SIZE 2000
#define PACKET_HEADER_SIZE 25
#define OVERSAMPLING_COUNT 8

class SensorSerializer
{
public:
	SensorSerializer(Dictionary<char, Sensor *> * label2Sensors, ActiveBufferedUSART * activeUSART);
	virtual ~SensorSerializer();


	void receivePattern();
	void setCurrentPattern(SamplingPattern * currentPattern);
	void executeCurrentPattern();

protected:

	Dictionary<char, Sensor *> * label2Sensors;
	ActiveBufferedUSART * activeUSART;
	SamplingPattern * currentPattern;
	TransmitActiveStringBuffer tbuf;
	ReceiveEOLActiveStringBuffer rbuf;


	char * packetHeader;
	const prog_mem * packetHeaderFormatString;
	const prog_mem * padding;
	RegularExpression patternRegex;

private:

    void serializePacket();

	void serializePacketHeader();

    void prepareTransmitterForSleep();




    void conversionComplete();
    volatile bool isConversionComplete;

    friend class SensorSerializerCallback;
};

#endif /* SensorSerializer_H_ */
